package com.mobileshop.model;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Vector;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.IAtom;
import com.jfinal.plugin.activerecord.Model;

public class YOrder  extends Model<YOrder>{
	public final static YOrder dao=new YOrder();
	private List<YOrderItem> orderItems=new ArrayList<YOrderItem>();
	public List<YOrderItem> getOrderItems() {
		if(orderItems.size()==0)
			orderItems=YOrderItem.dao.find("select * from t_order_item where ordercode='"+getStr("code")+"'");  
			return orderItems;
	}

	public void setOrderItems(Vector<YOrderItem> orderItems) {
		this.orderItems = orderItems;
	}

	
	public List<YOrder> getOrdersByCustomerCode(String customercode){
		List<YOrder> orders=find("select * from t_order where customercode="+customercode);
		return orders;
	}
	
	public void createOrder(){
		
		Db.tx(new IAtom() {
			
			@Override
			public boolean run() throws SQLException {
				YOrder.this.save();
				for(YOrderItem i :orderItems){
					i.set("code", "order_item_seq.nextval");
					i.save();
				}
				return true;
			}
		});
		
		
		
	}
}
